﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using PlatypusPlanner.Components.Base;
using PlatypusPlanner.Components.Controllers;
using PlatypusPlanner.Components.Database;

namespace PlatypusPlanner.Components.BLL
{
    public class Commons : BaseBLL
    {
        private static int DEFAULT_PASSWORD_SALT_LEN = 16;
        private static string DEFAULT_HASH_ALGORITHM = "SHA1";

        private CommonController _controller = null;
        public Commons(PlatypusEntities context = null)
            : base(context)
        {
            _controller = new CommonController(context);
        }

        #region password

        public static KeyValuePair<string, string> CreatePasswordAndSalt(string password)
        {
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[DEFAULT_PASSWORD_SALT_LEN];
            rng.GetBytes(buff);
            string salt = Convert.ToBase64String(buff);
            string saltAndPwd = string.Concat(password, salt);
            string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, DEFAULT_HASH_ALGORITHM);

            return new KeyValuePair<string, string>(hashedPwd, salt);
        }

        public static string GetHashedPassword(string password, string salt)
        {
            string saltAndPwd = string.Concat(password, salt);
            string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, DEFAULT_HASH_ALGORITHM);

            return hashedPwd;
        }

        #endregion


    }
}